﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HMS.BL;
using HMS.Common;
using HMS.Logging;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Collections.Specialized;

namespace HMS.DL
{
    public class DashboardData
    {

        private const string ClsName = "$DashboardData$";

        public string dimTableName { get; set; }
        public int ListId { get; set; }
        public string charttype { get; set; }
        public string chartfield { get; set; }

        public string GetData()
        {
            try
            {

                Connection Conn = new Connection();              
                string colField = string.Empty;
                //string strQry = string.Empty;

                //original before SP
                //if (dimTableName == "M_CONTACTS")
                //{
                   
                //    if (chartfield =="city") { colField ="primary_address_city"; }
                //    else if (chartfield == "state") { colField = "primary_address_state"; }

                //    strQry = " select " + colField + " 'Key',COUNT(" + colField + ") 'CNT'";
                //    strQry = strQry + " from " +  dimTableName ;
                //    strQry = strQry + " group by " + colField;                 
                //}
                //else
                //{

                //    if (chartfield == "city") { colField = "billing_address_city"; }
                //    else if (chartfield == "state") { colField = "billing_address_state"; }

                //    strQry = " select " + colField + " 'Key',COUNT(" + colField + ") 'CNT'";
                //    strQry = strQry + " from " + dimTableName;
                //    strQry = strQry + " group by " + colField;       
                //}

                //Util.WritInfoLog(ClsName, "GetData", "Query for GetData", string.Format("Query:{0}", strQry));
                //DataSet ds1 = Conn.GetDataSetFromFactory(strQry);
                //Util.WritInfoLog(ClsName, "GetData", "End Query for GetData");

                //Util.WritInfoLog(ClsName, "GetData", "Query for GetData", string.Format("Query:{0}", strQry));
                //DataSet ds1 = Conn.GetDataSetFromFactory(strQry);
                //Util.WritInfoLog(ClsName, "GetData", "End Query for GetData");

                if (dimTableName == "M_CONTACTS")
                {
                    if (chartfield == "city") { colField = "primary_address_city"; }
                    else if (chartfield == "state") { colField = "primary_address_state"; }                   
                }
                else
                {
                    if (chartfield == "city") { colField = "billing_address_city"; }
                    else if (chartfield == "state") { colField = "billing_address_state"; }                 
                }             

                List<DBParameter> lstparam = new List<DBParameter>();
                lstparam.Add(new DBParameter("@colField", DbType.String, ParameterDirection.Input, 255, colField));
                lstparam.Add(new DBParameter("@dimTableName", DbType.String, ParameterDirection.Input, 255, dimTableName));

                Util.WritInfoLog(ClsName, "GetData", "method for getting graph data");
                DataSet ds = Conn.GetDataSetFromFactoryProc("usp_GetGraphData", ref(lstparam));
                Util.WritInfoLog(ClsName, "GetData", "End method for getting graph data");

                Dictionary<string, int> chartData = new Dictionary<string, int>();

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    chartData.Add(dr["Key"].ToString(), Convert.ToInt32(dr["CNT"]));
                }

                JavaScriptSerializer js = new JavaScriptSerializer();
                return js.Serialize(chartData);

            }
            catch (Exception ex)
            {
                Util.WritErrorLog(ClsName, "GetData", "Error in GetData", "", ex);
                throw ex;
            }
        }


    }
}
